/*
 * spi_flash.h
 *
 *  Created on: May 27, 2010
 *      Author: Tyler Lalime
 */

#ifndef SPI_FLASH_H_
#define SPI_FLASH_H_
#ifndef __XC__
#include <xccompat.h>
#endif

#include "spi_master.h"

/*
 * flash_init()
 * Initializes the flash device for access.  Must be done first.
 */
#ifdef __XC__
void flash_init(port spi_ss, spi_master_interface &spi_if);
#else
void flash_init(port spi_ss, spi_master_interface *spi_if);
#endif

/*
 * flash_shutdown()
 * Closes the flash device.  Should be done when the device is
 * done being accessed.
 */
#ifdef __XC__
void flash_shutdown(spi_master_interface &spi_if);
#else
void flash_shutdown(spi_master_interface *spi_if);
#endif

/*
 * flash_write()
 * This function writes length bytes of data to the SPI
 * flash chip.  Returns the address of the data.
 */
#ifdef __XC__
int flash_write(port spi_ss, spi_master_interface &spi_if, unsigned char data[], int length, int &current_addr);
#else
int flash_write(port spi_ss, spi_master_interface *spi_if, unsigned char data[], int length, int *current_addr);
#endif

/*
 * flash_read()
 * This function reads length bytes of data beginning with the
 * address addr and fills it into data.
 */
#ifdef __XC__
void flash_read(port spi_ss, spi_master_interface &spi_if, unsigned char data[], unsigned int addr, int length);
void flash_clear(port spi_ss, spi_master_interface &spi_if, int start_addr[], int n, chanend os);
void wait_for_device_or_os(port spi_ss, spi_master_interface &spi_if, chanend os);
#else
void flash_read(port spi_ss, spi_master_interface *spi_if, unsigned char data[], unsigned int addr, int length);
void flash_clear(port spi_ss, spi_master_interface *spi_if, int start_addr[], int n, chanend os);
void wait_for_device_or_os(port spi_ss, spi_master_interface *spi_if, chanend os);
#endif

void spi_select(port spi_ss);
void spi_deselect(port spi_ss);

#endif /* SPI_FLASH_H_ */


